package com.angular.back.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.angular.back.entity.User;
import com.angular.back.service.UserServive;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@CrossOrigin(origins = {"http://localhost:4200","null"})
@RestController
public class HelloWorldController {
	@Resource(name="userService")
	UserServive userServive;
	Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
  @RequestMapping("/hello1")
  public String hello1() {
    return "Hello World";
  }
  
 
  @RequestMapping("/list")
  public @ResponseBody List<User> getUsers(){
	  return userServive.getUsers();
  }
  
  @RequestMapping("/getOneUser/{id}")
  public @ResponseBody User getOneUser(@PathVariable Long id){
	  return userServive.getOneUser(id);
  }
  
@RequestMapping("/addUser")
  public @ResponseBody String addUser(HttpServletRequest request, HttpServletResponse response){
	  String userInfo = request.getParameter("userInfo");
	  User user=gson.fromJson(userInfo, User.class);
	  userServive.addUser(user);
	  return gson.toJson("OK");
  }
  
  @RequestMapping("/delUser/{id}")
  public @ResponseBody String delUser(@PathVariable Long id){
	  userServive.delUser(id);
	  return gson.toJson("OK");
  }

}
